package cn.org.donald.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.org.donald.framework.pojo.dto.AddRoleMenuDto;
import cn.org.donald.framework.util.ListUtil;
import cn.org.donald.mapper.MenuMapper;
import cn.org.donald.pojo.entity.Menu;
import cn.org.donald.pojo.entity.RoleMenu;
import cn.org.donald.mapper.RoleMenuMapper;
import cn.org.donald.framework.service.SysRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Donald
 * @since 2020-11-10
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements SysRoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    @Transactional
    public boolean insertBatch(AddRoleMenuDto addRoleMenuDto) {
        List<Menu> menuList = addRoleMenuDto.getMenuList();
        List<RoleMenu> roleMenuList = new ArrayList<>();
        menuList.forEach(menu -> {
            if (menu.getParentId()!=null){
                QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("menu_id",menu.getParentId());
                queryWrapper.eq("role_id",addRoleMenuDto.getRoleId());
                List<RoleMenu> roleMenus = this.list(queryWrapper);
                if (ListUtil.isEmptyOrHasClassFieldAllEmpty(roleMenus)){
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setRoleId(addRoleMenuDto.getRoleId());
                    roleMenu.setMenuId(menu.getParentId());
                    this.save(roleMenu);
                }
            }
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(menu.getId());
            roleMenu.setRoleId(addRoleMenuDto.getRoleId());
            roleMenuList.add(roleMenu);
        });
        return saveBatch(roleMenuList);

    }

    @Override
    @Transactional
    public boolean delete(RoleMenu roleMenu) {
        List<Menu> menuList = findMenuByParentId(roleMenu.getMenuId());
        if (!ListUtil.isEmptyOrHasClassFieldAllEmpty(menuList)){
            menuList.forEach(menu -> {
                RoleMenu roleMenu1 = new RoleMenu();
                roleMenu1.setMenuId(menu.getId());
                roleMenu1.setRoleId(roleMenu.getRoleId());
                QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>(roleMenu1);
                this.remove(queryWrapper);
            });
        }
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>(roleMenu);
        return this.remove(queryWrapper);
    }

    private List<Menu> findMenuByParentId(Long id) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        return menuMapper.selectList(queryWrapper);
    }


}
